<?php

namespace App\Jobs\Circle;

use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Models\Circle\WxCircle;
use App\Models\Posts\WxUserCircle;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdateCircleCountJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $circle_id = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($circle_id)
    {
        //
        $this->circle_id = $circle_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(_empty_($this->circle_id)){
            return;
        }

        $comment_count = WxComment::where('posts_circle_id', $this->circle_id)->where('comment_state', 1)->count();
        $pay_content_count = WxPost::where('circle_id', $this->circle_id)->where('pay_content_id', '>', 0)->where('is_examine', 1)
            ->where('posts_state', 0)->count();
        $circle_posts_count = WxPost::where('circle_id',$this->circle_id)->where('is_examine', 1)->where('posts_state', 0)->count();
        $circle_follow_count = WxUserCircle::where('circle_id',$this->circle_id)->where('user_circle_state', 0)->count();
        WxCircle::withoutSyncingToSearch(function () use ($comment_count, $pay_content_count, $circle_posts_count, $circle_follow_count) {
            WxCircle::where('id', $this->circle_id)->update([
                'count_comment' => $comment_count,
                'count_pay_content'=>$pay_content_count,
                'count_circle_posts'=>$circle_posts_count,
                'count_circle_follow'=>$circle_follow_count
            ]);
        });
    }
}
